package com.bb.costtracker.entities.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.dao.DataAccessException;

public class BaseDao<T> {
	private SessionFactory sessionFactory;
	
	public BaseDao(SessionFactory sessionFactory) {
		super();
		this.sessionFactory = sessionFactory;
	}

	public void save(Object entity) throws DataAccessException {
		Session session = sessionFactory.openSession();
		Transaction tr = session.beginTransaction();
		
		session.save(entity);
		
		tr.commit();
	}

	public void update(Object entity) throws DataAccessException {
		Session session = sessionFactory.openSession();
		Transaction tr = session.beginTransaction();
		
		session.update(entity);
		
		tr.commit();
	}

	public void saveOrUpdate(Object entity) throws DataAccessException {
		Session session = sessionFactory.openSession();
		Transaction tr = session.beginTransaction();
		
		session.saveOrUpdate(entity);
		
		tr.commit();
	}
	
}
